clear all

% Simulate no change (gamma = 0), benchmark

global Dividend

Dividend=0;
%
% Given Par2 optimal:
n = 50; tau = 0.1;
r = 0.036;
Par1 = zeros(25,1);
load ParEst_eh1_time0_Mex ParEst_eh1_time0_Mex
Par1(1:24) = ParEst_eh1_time0_Mex;
Par1(21) = 10^Par1(21);
Par1(22:24) = Par1(22:24)*1000;
Par1(25) = 0;
gamma =  Par1(25); a = Par1(22); alphaf = Par1(17); betaf = Par1(18); alphai = Par1(19); betai = Par1(20); theta = Par1(21);
b1 = Par1(23); b2 = Par1(24);
load W_time1_age0_eh1_comp11.out
w_f_g=W_time1_age0_eh1_comp11; % Read wages, g and f distributions
wf_1=w_f_g(:,1);        % Wage in formal sector
wi_1=w_f_g(:,2);        % Wage in informal sector
wf_2=w_f_g(:,7);      % Wage in formal sector
wi_2=w_f_g(:,8);      % Wage in informal sector

[Ff_1,ff_1,Fi_1,fi_1,Ff_2,ff_2,Fi_2,fi_2]=solve_wage_offer_dist(Par1,wf_1,wi_1,wf_2,wi_2,n);
[Wff,Wfi,Wfn,Wif,Wnf,Wii,Win,Wni,Wnn,Rw_fi_ni, Rw_ii_ni, Rw_ii_in, Rw_if_in,...
     Rw_fi_fn, Rw_ff_fn, Rw_if_nf, Rw_ff_nf, Rw_in_nn, Rw_fn_nn, Rw_ni_nn, Rw_nf_nn]=solve_value_function_A3_C(Par1,Ff_1,wf_1,Fi_1,wi_1,Ff_2,wf_2,Fi_2,wi_2,n);
[HHstates,wage_moments,transitions,gff, gfi, gif, gii, gfn, gnf, gin, gni]=data_simulation_A12_end(Par1,ff_1,wf_1,fi_1,wi_1,ff_2,wf_2,fi_2,wi_2,Wff,Wfi,Wfn,Wif,Wnf,Wii,Win,Wni,Wnn);
Rw_1_f_gamma_0_GE = Rw_fi_ni; Rw_1_i_gamma_0_GE = Rw_ii_ni;  Rw_2_i_gamma_0_GE = Rw_ii_in; Rw_2_f_gamma_0_GE = Rw_if_in;


save Rw_1_f_gamma_0_GE Rw_1_f_gamma_0_GE
save Rw_2_f_gamma_0_GE Rw_2_f_gamma_0_GE
save Rw_1_i_gamma_0_GE Rw_1_i_gamma_0_GE
save Rw_2_i_gamma_0_GE Rw_2_i_gamma_0_GE

mff=HHstates(1);
mfi=HHstates(2);
mfn=HHstates(3);
mif=HHstates(4);
mnf=HHstates(5);
mii=HHstates(6);
min_=HHstates(7);
mni=HHstates(8);
mnn=HHstates(9);

% Constructing produtivities for plotting Log(wage) x Log(productivity)

% marginal distributions
mf_1=mff+mfi+mfn;
mi_1=mif+mii+min_;
mf_2=mff+mif+mnf;
mi_2=mfi+mii+mni;


% firm size
el_f_1=mf_1*gf_1./ff_1; 
% el_f_1(1)=el_f_1(2); % may need smoothing
% el_f_1(n)=el_f_1(n-1);
el_i_1=mi_1*gi_1./fi_1;
% el_i_1(1)=el_i_1(2); % may need smoothing
% el_i_1(n)=el_i_1(n-1);
el_f_2=mf_2*gf_2./ff_2; 
% el_f_2(1)=el_f_2(2); % may need smoothing
% el_f_2(n)=el_f_2(n-1);
el_i_2=mi_2*gi_2./fi_2;
% el_i_2(1)=el_i_2(2); % may need smoothing
% el_i_2(n)=el_i_2(n-1);

wht_f_1=(wf_1(n)-wf_1(1))/(n-1); 
wht_i_1=(wi_1(n)-wi_1(1))/(n-1);
wht_f_2=(wf_2(n)-wf_2(1))/(n-1);
wht_i_2=(wi_2(n)-wi_2(1))/(n-1);

der_el_f_1=[Inf;el_f_1(2:n)-el_f_1(1:n-1)]/wht_f_1;
der_el_i_1=[Inf;el_i_1(2:n)-el_i_1(1:n-1)]/wht_i_1;
der_el_f_2=[Inf;el_f_2(2:n)-el_f_2(1:n-1)]/wht_f_2;
der_el_i_2=[Inf;el_i_2(2:n)-el_i_2(1:n-1)]/wht_i_2;

% productivity 
p_f_1=(wf_1+el_f_1./der_el_f_1)*(1 + tau);
p_i_1=wi_1+el_i_1./der_el_i_1;
p_f_2=(wf_2+el_f_2./der_el_f_2)*(1 + tau);
p_i_2=wi_2+el_i_2./der_el_i_2;

% M (total employed workforce) in 2005 = 41441076
% Fraction of firms in the formal sector in 2002 includes self-emp. (ENAMIN/INEGI: 23.7%)
% Number of formal firms (IMSS registry, BOSCH AND CAMPOS-VASQUEZ 2005): 800,000
% Number of firms = 800000/0.237
M_N=41441076/(800000/0.237);

pi_f_1=(p_f_1-wf_1*(1+tau)).*el_f_1*M_N/0.237;
pi_i_1=(p_i_1-wi_1).*el_i_1*M_N/0.763;

save fitted_model p_f_1 p_i_1 p_f_2 p_i_2 Ff_1 Fi_1 Ff_2 Fi_2 ff_1 fi_1 ff_2 fi_2



